package com.example.aatg.tc;

import com.example.aatg.tc.TemperatureConverter.OP;

import android.app.Activity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;

public class TemperatureConverterActivity extends Activity {
    public EditNumber mCelsius;
	public EditNumber mFahrenheit;

	/**
	 * @author kosovar
	 *
	 */
	public class TemperatureChangedWatcher implements TextWatcher {

		private final EditNumber mSource;
		private final EditNumber mDest;
		private final OP mOp;
		
		public TemperatureChangedWatcher(TemperatureConverter.OP op) {
			if (op == OP.C2F) {
				this.mSource = mCelsius;
				this.mDest = mFahrenheit;
			}
			else {
				this.mSource = mFahrenheit;
				this.mDest = mCelsius;
			}
				
			mOp = op;
		}
		
		/* (non-Javadoc)
		 * @see android.text.TextWatcher#beforeTextChanged(java.lang.CharSequence, int, int, int)
		 */
		@Override
		public void beforeTextChanged(CharSequence s, int start, int count,
				int after) {
		}

		/* (non-Javadoc)
		 * @see android.text.TextWatcher#onTextChanged(java.lang.CharSequence, int, int, int)
		 */
		@Override
		public void onTextChanged(CharSequence s, int start, int before,
				int count) {
			if (!mDest.hasWindowFocus() || mDest.hasFocus() || s == null) {
				return;
			}
			final String str = s.toString(); 
			if ("".equals(str)) {
				mDest.setText("");
				return;
			}
			try {
				final double temp = Double.parseDouble(str);
				final double result = (mOp == OP.C2F) ?
					TemperatureConverter.celsiusToFahrenheit(temp) :
					TemperatureConverter.fahrenheitToCelsius(temp);
				final String resultString = String.format("%.2f", result);
				mDest.setNumber(result);
				mDest.setSelection(resultString.length());
			} catch (NumberFormatException e) {
			} catch (Exception e) {
				mSource.setError("ERROR: " + e.getLocalizedMessage());
			}
		}

		/* (non-Javadoc)
		 * @see android.text.TextWatcher#afterTextChanged(android.text.Editable)
		 */
		@Override
		public void afterTextChanged(Editable s) {
		}
	}

	/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        mCelsius = (EditNumber)findViewById(R.id.celsius);
        mFahrenheit = (EditNumber)findViewById(R.id.fahrenheit);
        
        mCelsius.addTextChangedListener(new TemperatureChangedWatcher(OP.C2F));
        mFahrenheit.addTextChangedListener(new TemperatureChangedWatcher(OP.F2C));
    }
}